package com.test.sdk.scripts.jobs;

import com.test.sdk.scripts.dao.OnlineUserDAO;
import com.test.sdk.scripts.pojo.QuartzScripts;
import com.test.sdk.scripts.service.QuartzScriptsService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Calendar;
import java.util.Date;

/**
 * @author 徒有琴
 */
@Component
public class OnlineUserJob implements Job {

    private static OnlineUserDAO onlineUserDAO;
    private static QuartzScriptsService quartzScriptsService;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        System.out.println("===OnlineUserJob====");
        Calendar calendar = Calendar.getInstance();
        //calendar.setTime(new Date());
        calendar.add(Calendar.MINUTE, -20);
        Integer id = (Integer) jobExecutionContext.getJobDetail().getJobDataMap().get("id");
        System.out.println(id);
        QuartzScripts scripts = new QuartzScripts();
        scripts.setId(id);
        scripts.setLastExecute(new Date());
        try {
            onlineUserDAO.deleteTimeOutUser(calendar.getTime());
            scripts.setLastResult("成功");
        } catch (Exception e) {
            e.printStackTrace();
            scripts.setLastResult(e.getMessage());
        }
        quartzScriptsService.update(scripts);
    }

    @Autowired
    public void setOnlineUserDAO(OnlineUserDAO onlineUserDAO) {
        OnlineUserJob.onlineUserDAO = onlineUserDAO;
    }

    @Autowired
    public void setQuartzScriptsService(QuartzScriptsService quartzScriptsService) {
        OnlineUserJob.quartzScriptsService = quartzScriptsService;
    }
}
